package com.bw.seckill.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.common.MD5Util;
import com.bw.common.ResponseResult;
import com.bw.common.auth.JwtUtils;
import com.bw.common.auth.RsaUtils;
import com.bw.common.auth.UserInfo;
import com.bw.seckill.entity.TbSeller;
import com.bw.seckill.service.ITbSellerService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.security.PrivateKey;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xingjinning
 * @since 2020-07-14
 */
@RestController
@RequestMapping("/seller")
public class TbSellerController {

    @Value("${keyPath}")
    private String keyPath;
    @Autowired
    private ITbSellerService sellerService;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @PostMapping("login")
    public ResponseResult login(@RequestBody TbSeller tbSeller, HttpServletResponse response){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            Map map = new HashMap<>();
            map.put("loginName",tbSeller.getLoginName());
            map.put("pwd", MD5Util.md5Encrypt32Lower(tbSeller.getPwd()));
            queryWrapper.allEq(map);
            List<TbSeller> tbSellers = sellerService.list(queryWrapper);
            if(tbSellers.size()>0){
                TbSeller seller = tbSellers.get(0);
//                PrivateKey privateKey = RsaUtils.getPrivateKey(keyPath + "rsa.pri");
                UserInfo userInfo = new UserInfo();
                userInfo.setId(seller.getId());
                userInfo.setUsername(seller.getSellerName());
//                String token = JwtUtils.generateToken(userInfo, privateKey, 60);
//                Map mapinfo = new HashMap();
//                mapinfo.put("seller",seller);
//                mapinfo.put("token",token);
//                result.setMessage("登陆成功");
//

                String token= UUID.randomUUID().toString();//生成随机token
                Cookie cookie = new Cookie("token",token);
                cookie.setMaxAge(1800);//超时时间
                cookie.setPath("/");
                response.addCookie(cookie);
                //将用户信息存入redis
                redisTemplate.opsForValue().set(token,userInfo);
                redisTemplate.expire(token,30, TimeUnit.MINUTES);
                Map map1 = new HashMap<>();
                map1.put("token",token);
                map1.put("user",userInfo);
                result.setResult(map1);

            }else{
                result.setMessage("用户名或密码错误");
                result.setSuccess(false);
            }
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("登录异常"+e.toString());
        }
        return result;
    }
}
